package com.cade.agriculturemall.view.shoppingCar;

import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.model.ShopcarsModel;
import com.cade.agriculturemall.model.ShoppingCarsEntity;
import com.cade.agriculturemall.model.UserInfoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/12/14.
 */

public class ShoppingCarPresenter extends BasePresenter<ShoppingCarContract.IView> implements ShoppingCarContract.IPesenter {

    private String currentUserId = "";

    private AVQuery<ShoppingCarsEntity> shoppingCarsEntityAVQuery = new AVQuery<>("ShoppingCars");

    public ShoppingCarPresenter(ShoppingCarContract.IView view) {
        super(view);
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public String getUserId() {
        return null;
    }

    @Override
    public void downLoadShopCars() {
        AVUser.alwaysUseSubUserClass(UserInfoEntity.class);
        final UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);
        currentUserId = user.getObjectId();
        if (currentUserId != null) {
            AVQuery<ShoppingCarsEntity> shoppingCarsEntityAVQuery = new AVQuery<>("ShoppingCars");
            shoppingCarsEntityAVQuery.whereEqualTo("user_Id", currentUserId);
            shoppingCarsEntityAVQuery.whereEqualTo("orderId", null);
            shoppingCarsEntityAVQuery.addAscendingOrder("shop_Id");
            shoppingCarsEntityAVQuery.include("pro");
            shoppingCarsEntityAVQuery.findInBackground(new FindCallback<ShoppingCarsEntity>() {
                @Override
                public void done(List<ShoppingCarsEntity> list, AVException e) {

                    List<ShopcarsModel> goodEntities = new ArrayList<>();
                    if (e == null) {
                        int size = 0;
                        if (list != null)
                            size = list.size();
                        for (ShoppingCarsEntity shoppingCarsEntity : list) {
                            ShopcarsModel cartModel = null;
                            for (ShopcarsModel model : goodEntities) {
                                if (model.getShopId().equals(shoppingCarsEntity.getShoper())) {
                                    cartModel = model;
                                    break;
                                }
                            }
                            if (cartModel == null) {
                                cartModel = new ShopcarsModel();
                                goodEntities.add(cartModel);
                                cartModel.setShopId(shoppingCarsEntity.getShoper());
                                cartModel.setGoodBeanList(new ArrayList());
                            }
                            cartModel.getGoodBeanList().add(shoppingCarsEntity);
                        }
                        mView.onLoadShopCars(goodEntities, size);
                    }
                    else {
                        mView.showMessage(e.getMessage());
                    }
                }
            });
        }
    }

    @Override
    public void upLoadShopCars(String shopCarId) {

    }


}
